home *** CD-ROM | disk | FTP | other *** search
- /*
- MultiWin TE/Undo/S_H Demo 1.0.1
- By Eric Long.
-
- Original Show_Help code by James Walker. This version modified by Eric Long.
-
- Changes in 1.0.1: Corrected assignment of var windowID in HandleKeyDown
- routine; made it a long.
-
- This project was compiled with THINK C v.7.x without Universal Headers.
- I could not get the code to compile under Universal Headers.
- I received an error saying "ScrapInfo" was undefined. ???
-
- If Mac file names were longer, this project would be titled:
-
- Multiple (Dialog)Window TextEdit and Edit Menu Handling w/ Undo,
- plus a modified modeless version of Show_Help (original code
- by James Walker) Demo.
-
- I shortened up the title the best I could. (-:
-
- This project was launched to teach myself a number of things. I hadn't
- handled multiple windows with TE items before, nor had I ever supported the
- Edit menu myself, and I needed to develop code to handle Undo. I also wanted
- to make James Walker's Show_Help code work as a modeless dialog. This
- archive is distributed with his permission.
-
- I wasn't able to locate code examples of handling Undo. I'm sure it
- will help to have this available for others to peek at. I also know
- from my experience that a lot of code examples that are available are
- difficult for those learning to assimilate. I haven't yet learned all
- those fancy compiler techniques with the #pragmas and whatnot, I am
- still following the formatting I learned when I first read Dave Mark's
- "Mac Programming Primer Vol.1", I don't know the benefits of assigning
- vars to registers, nor do I write inline assembler as yet. Those things
- considered, I figure my code is very easy for learners to follow! No
- doubt all of us learners that stick with it will someday be writing code
- that takes advantage of all those tricks, but until then, here's some code
- you can read and understand!
-
- James Walker's Show_Help code is fairly complex. It's helped me learn a
- great deal. It's also taken me awhile to understand it. I think if anyone
- wants to dig into TE, he provides a lot to chew on... _and_ if I keep
- reading DEVELOP, I may even figure it all out someday! I tried to keep
- the routines for the modeless Show_Help all together in the same source file.
- I created some similiar routines in this source file (FlashButton). I know
- it is redundant, but I thought it would make it easy to seperate the two, if
- you want to weed one out from the other.
-
- Show_Help is in the public domain. This code is entered as well. Use it,
- or any part of it, as you like; free and clear. It will require some minor
- changes to be 6 compatible. You'll need to add code to position your windows
- and you might need to check for WaitNextEvent trap. I'm not sure if that is
- available in 6. SystemTask() will help you in conjunction with GetNextEvent
- in that case. I don't think there's much else that would conflict with 6.
-
- Best Regards. Eric
- */
-
- /* konstants */
-
- #define kBaseResID 128
- #define kSleep 20L
- #define k255Characters 255
- #define kMaxTextPosition 32767
- #define kGenericDlgStorage (sizeof(DialogRecord))
-
- #define RETURN_CHAR 0x0D
- #define TILDE_CHAR 0x7E
- #define ENTER_CHAR 0x03
- #define ESCAPE_CHAR 0x1B
- #define OPTION_KEY 0x3A
-
- #define sEmptyString "\p"
-
- #define kDefaultButton 1
- #define kCancelButton 2
- #define kOnlyButton 3
-
- #define kAboutAlertID 500
- #define rErrorAlertID 501
- #define rShowHelpDlg 128
-
- // Items for both generic dialogs
- #define iDefaultOutline 2
- #define iEditText 5
-
- #define rGenericDlg1 129
- #define iRadioYes 6
- #define iRadioNo 7
- #define iCheckBox 9
-
- #define rGenericDlg2 130
- #define iRadioFirstDay 6
- #define iFirstCheckBox 14
-
-
- /* Window Stuff */
- #define kMoveToFront (WindowPtr)-1L
- #define kMax_WindowsOpen 10
-
- /* Menus */
- #define mApple kBaseResID+1
- #define iAbout 1
- #define mFile kBaseResID+2
- #define iOpenGenDlg1 1
- #define iOpenGenDlg2 2
- #define iClose 4
- #define iHelp 6
- #define iQuit 8
- #define mEdit kBaseResID
- #define iUndo 1
- #define iCut 3
- #define iCopy 4
- #define iPaste 5
- #define iClear 6
- #define iSelectAll 8
-
-
- /* Help screen Info */
- #define kHelp_info_id kBaseResID
-
- /* Error Strings */
- #define rErrStringsID 128
- #define eCantOpenNewWindow 1
- #define eNotEnoughMem 2
- #define eDialogNotFound 3
-
-
- typedef struct{
- Str255 undoString;
- short undoItem;
- short undoSelStart;
- short undoSelEnd;
- } My_UndoRec;
-
-
- /************************* Globals ******************************/
-
- Boolean gDone, gCanUndo = false;
- short gWindowsOpen = 0; // Total #windows presently open
- TEHandle gCur_DlgText; // Currently active TE item
- My_UndoRec gUndo_Info;
-
-
- /************************ Prototypes **********************************/
-
- void main( void );
- void ToolBoxInit ( void );
- void MenuBarInit( void );
- void ScrapInit( void );
- void EventLoop ( void );
- void DoEvent( EventRecord *eventPtr );
- void DoUpdate( WindowPtr window );
- void HandleNull( EventRecord *event );
- void HandleActivate( EventRecord *eventPtr );
- void HandleOSEvent( EventRecord *eventPtr );
- void HandleKeyDown(EventRecord *eventPtr);
- void HandleMouseDown (EventRecord *eventPtr );
- void HandleMenuChoice( EventRecord *event );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void HandleEditChoice( EventRecord *event, short item );
- pascal void buttonProc( DialogPtr theDialog, short item );
- void Init_GenericDlg1( void );
- void Init_GenericDlg2( void );
- void Handle_GenericDialog( EventRecord *event, long whichDlg );
- void ProcessEvt_GenericDlg1( DialogPtr dialog, short iHit );
- void ProcessEvt_GenericDlg2( DialogPtr dialog, short iHit );
- void Close_GenericDialog(DialogPtr dialog);
- void Adjust_Generic_EditMenu( DialogPtr dialog );
- void Do_Undo( DialogPtr dialog );
- My_UndoRec Get_UndoInfo(DialogPeek dialog);
- void Maintain_EditMenu( DialogPtr dialog, My_UndoRec *preEditState );
- void AdjustMenu( short menuID, short item, Boolean switchOn );
- Boolean Keydown_Button( EventRecord *event, short item_number,
- short buttonKind);
- void FlashButton( short item_number );
- void Close_AllDialogs( void ); // Option-click in goAway, or Quit
- short isPressed(unsigned short k );
- Boolean Can_OpenMoreWindows( void );
- void DoError( short err, Boolean fatal );
-
- /* Prototypes: For Modeless Show_Help */
- void Init_Show_help( short help_info_id );
- void Handle_ShowHelp( EventRecord *event, DialogPtr dialog );
- void Close_ShowHelp(DialogPtr dptr);
- void Do_ShowHelp_Copy( DialogPtr showHelpDlg );
-
-
-
- /******************** main ***********************************/
-
- void main ( void )
- {
- ToolBoxInit ();
- MaxApplZone();
- MenuBarInit();
- ScrapInit();
-
- FlushEvents( everyEvent, 0 );
- EventLoop();
- LoadScrap(); // Make sure scrap is in memory, not left out on a disk
- }
-
-
- /************************* ToolBoxInit ******************************/
-
- void ToolBoxInit ( void )
- {
- InitGraf ( &thePort );
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ( nil );
- InitCursor ();
- }
-
-
- /*********************** MenuBarInit ***************************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
- Str255 directionStr;
-
- menuBar = GetNewMBar( kBaseResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
- }
-
-
- /************************** ScrapInit *******************************/
-
- void ScrapInit( void )
- {
- if (ScrapInfo.scrapState < 0)
- ZeroScrap();
- else
- TEFromScrap(); // Copy Desk Scrap into TE Scrap
-
- gUndo_Info.undoString[0] = 0;
- }
-
-
- /************************ EventLoop ************************/
-
- void EventLoop ( void )
- {
- EventRecord event;
- WindowPtr dialog;
-
- gDone = false;
-
- while ( !gDone )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, nil) )
- DoEvent ( &event );
- else
- HandleNull( &event );
- }
- }
-
-
- /************************** DoEvent ***********************/
-
- void DoEvent( EventRecord *eventPtr )
- {
- WindowPtr theWindow;
-
- switch ( eventPtr->what )
- {
- case activateEvt:
- HandleActivate(eventPtr);
- break;
- case keyDown:
- case autoKey:
- HandleKeyDown(eventPtr);
- break;
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case updateEvt:
- DoUpdate((WindowPtr)eventPtr->message);
- break;
- case osEvt:
- HandleOSEvent( eventPtr );
- break;
- }
- }
-
-
- /********************* DoUpdate ******************************/
-
- void DoUpdate( WindowPtr window )
- {
- GrafPtr savePort;
-
- GetPort(&savePort);
- SetPort(window);
-
- BeginUpdate( window );
-
- if (((WindowPeek)(window))->windowKind == dialogKind)
- UpdtDialog((DialogPtr)window, window->visRgn);
-
- EndUpdate( window );
-
- SetPort(savePort);
- }
-
-
- /******************* HandleNull **************************/
-
- void HandleNull( EventRecord *eventPtr )
- {
- GrafPtr thePort;
- long windowID;
-
- GetPort(&thePort);
-
- windowID = GetWRefCon(thePort);
- switch (windowID)
- {
- case rShowHelpDlg:
- Handle_ShowHelp( eventPtr, (DialogPtr)thePort ); // TEIdle
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- Handle_GenericDialog(eventPtr, windowID );
- break;
- }
- }
-
-
- /************************ HandleActivate *******************************/
-
- void HandleActivate( EventRecord *eventPtr )
- {
- WindowPtr theWindow;
- DialogPeek dlgPeek;
- short iType;
- Rect iRect;
-
- theWindow = (WindowPtr)(eventPtr->message);
-
-
- if (IsDialogEvent(eventPtr))
- {
- switch (GetWRefCon(theWindow))
- {
- case rShowHelpDlg:
- Handle_ShowHelp(eventPtr, (DialogPtr)theWindow);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- dlgPeek = (DialogPeek)theWindow;
- if (eventPtr->modifiers & activeFlag)
- {
- SetPort(theWindow);
- gCur_DlgText = (dlgPeek->textH);
- TEActivate(gCur_DlgText);
- Adjust_Generic_EditMenu((DialogPtr)theWindow );
- }
- else
- {
- TEDeactivate((dlgPeek->textH));
- // Maintain Undo
- gCanUndo = false;
- }
- break;
- }
- }
- }
-
-
- /************************ HandleOSEvent *******************************/
-
- void HandleOSEvent( EventRecord *eventPtr )
- {
- WindowPtr theWindow;
- DialogPeek dlgPeek;
- short iType;
- Rect iRect;
-
- theWindow = FrontWindow();
- switch (GetWRefCon(theWindow))
- {
- case rShowHelpDlg:
- Handle_ShowHelp(eventPtr, (DialogPtr)theWindow);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- dlgPeek = (DialogPeek)theWindow;
- if (eventPtr->message & resumeFlag)
- {
- gCur_DlgText = (dlgPeek->textH);
- TEActivate(gCur_DlgText);
- Adjust_Generic_EditMenu((DialogPtr)theWindow );
- }
- else
- {
- TEDeactivate((dlgPeek->textH));
- // Maintain Undo
- gCanUndo = false;
- }
- break;
- }
- // Update desk scrap or receive update from desk scrap
- if (eventPtr->message & resumeFlag)
- TEFromScrap();
- else
- {
- ZeroScrap();
- TEToScrap();
- }
- }
-
-
- /******************** HandleKeyDown ****************************/
-
- void HandleKeyDown(EventRecord *eventPtr)
- {
- WindowPtr window;
- long windowID;
-
- if ((eventPtr->modifiers & cmdKey) != 0)
- HandleMenuChoice( eventPtr );
- else
- {
- window = FrontWindow();
- windowID = GetWRefCon(window);
- switch (windowID)
- {
- case rShowHelpDlg:
- Handle_ShowHelp( eventPtr, window);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- Handle_GenericDialog( eventPtr, windowID);
- break;
- }
- }
- }
-
-
- /******************* HandleMouseDown **************************/
-
- void HandleMouseDown ( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long windowID;
- long the_text;
- DialogPeek theDialogPtr;
-
- thePart = FindWindow ( eventPtr->where, &window );
- windowID = GetWRefCon(window);
- if ((thePart >= inContent) && (window != (WindowPtr)thePort))
- SelectWindow(window);
- else
- {
- switch ( thePart )
- {
- case inMenuBar:
- HandleMenuChoice( eventPtr );
- break;
- case inSysWindow :
- SystemClick ( eventPtr, window );
- break;
- case inDrag :
- DragWindow ( window, eventPtr->where,
- &screenBits.bounds );
- break;
- case inGoAway:
- if (TrackGoAway(window, eventPtr->where))
- {
- if (isPressed(OPTION_KEY)) // Option-click in goAway, close everything
- Close_AllDialogs();
- else
- {
- switch(windowID)
- {
- case rShowHelpDlg:
- Close_ShowHelp((DialogPtr)window);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- Close_GenericDialog((DialogPtr)window);
- break;
- }
- }
- }
-
- if (!gWindowsOpen)
- AdjustMenu(mFile, iClose, false);
- break;
- case inContent:
- switch (windowID)
- {
- case rShowHelpDlg:
- Handle_ShowHelp(eventPtr, (DialogPtr)window );
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- Handle_GenericDialog(eventPtr, windowID);
- break;
- }
- break;
- }
- }
- }
-
-
- /********************** HandleMenuChoice **************************/
-
- void HandleMenuChoice( EventRecord *event )
- {
- long menuChoice;
- short menu;
- short item;
- char theChar;
-
- if ((event->what == keyDown) || (event->what == autoKey))
- {
- theChar = event->message & charCodeMask;
- menuChoice = (MenuKey(theChar));
- }
- else
- menuChoice = MenuSelect( event->where );
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- case mEdit:
- HandleEditChoice( event, item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /********************* HandleAppleChoice ***************************/
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- Alert(kAboutAlertID, nil);
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /********************* HandleFileChoice ****************************/
-
- void HandleFileChoice( short item )
- {
- long the_text, windowID;
- DialogPeek theDialogPtr;
-
- switch(item)
- {
- case iOpenGenDlg1:
- if (Can_OpenMoreWindows())
- Init_GenericDlg1();
- break;
- case iOpenGenDlg2:
- if (Can_OpenMoreWindows())
- Init_GenericDlg2();
- break;
- case iClose:
- windowID = GetWRefCon(thePort);
- switch(windowID)
- {
- case rShowHelpDlg:
- Close_ShowHelp((DialogPtr)thePort);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- Close_GenericDialog((DialogPtr)thePort);
- break;
- }
- break;
- case iHelp:
- if (Can_OpenMoreWindows())
- Init_Show_help( kHelp_info_id );
- break;
- case iQuit:
- if (gWindowsOpen)
- Close_AllDialogs();
- gDone = true;
- break;
- }
- }
-
-
- /********************* HandleEditChoice ******************************/
-
- void HandleEditChoice( EventRecord *event, short item )
- {
- DialogPtr activeWindow;
- long windowID;
-
- activeWindow = FrontWindow();
-
- if (activeWindow)
- {
- windowID = GetWRefCon(activeWindow);
- switch (item)
- {
- case iUndo:
- if (gCanUndo)
- Do_Undo(activeWindow);
- break;
- case iCut:
- gUndo_Info = Get_UndoInfo(activeWindow);
- gCanUndo = true;
- DlgCut(activeWindow);
- Adjust_Generic_EditMenu(activeWindow);
- break;
- case iCopy:
- switch (windowID)
- {
- case rShowHelpDlg:
- Do_ShowHelp_Copy(activeWindow);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- DlgCopy(activeWindow);
- Adjust_Generic_EditMenu(activeWindow);
- break;
- }
- break;
- case iPaste:
- gUndo_Info = Get_UndoInfo(activeWindow);
- gCanUndo = true;
- DlgPaste(activeWindow);
- Adjust_Generic_EditMenu(activeWindow);
- break;
- case iClear:
- gUndo_Info = Get_UndoInfo(activeWindow);
- gCanUndo = true;
- DlgDelete(activeWindow);
- Adjust_Generic_EditMenu(activeWindow);
- break;
- case iSelectAll:
- SelIText(activeWindow, (((DialogPeek)activeWindow)->editField)+1,
- 0, kMaxTextPosition);
- Adjust_Generic_EditMenu(activeWindow);
- break;
- }
- }
- }
-
-
- /********************* buttonProc **************************/
-
- pascal void buttonProc( DialogPtr theDialog, short theItem)
- {
- short type;
- Rect box;
- Handle itemHdl;
- long response, theColor;
-
- // Outline dialog default Button
- GetDItem(theDialog, ok, &type, &itemHdl, &box);
-
- PenSize(3,3);
- InsetRect( &box, -4, -4);
- FrameRoundRect( &box, 16, 16);
- PenNormal();
- }
-
-
- /************************ Init_GenericDlg1 ****************************/
-
- void Init_GenericDlg1( void )
- {
- short iType;
- Handle iHandle;
- Rect iRect;
- DialogPtr dialog;
- DialogPeek dlgPeek;
- Ptr dStorage;
-
- /* #define rGenericDlg1 129
- #define iDefaultOutline 2
- #define iEditText 5
- #define iRadioYes 6
- #define iRadioNo 7
- #define iCheckBox 9 */
-
-
- dStorage = NewPtrClear(kGenericDlgStorage);
-
- if (! dStorage)
- DoError(eNotEnoughMem, false);
- else
- {
- if (! gWindowsOpen)
- AdjustMenu(mFile, iClose, true);
- gWindowsOpen++;
-
- dialog = GetNewDialog(rGenericDlg1, dStorage, kMoveToFront);
- if (dialog)
- {
- SetPort(dialog);
-
- // Default Button Outline
- GetDItem(dialog, iDefaultOutline, &iType, &iHandle, &iRect);
- SetDItem(dialog, iDefaultOutline, iType, buttonProc, &iRect);
-
- // Set Insertion Caret
- SelIText(dialog, iEditText, 0,0);
- dlgPeek = (DialogPeek)dialog;
- gCur_DlgText = (dlgPeek->textH);
-
- // Default Radio setting
- GetDItem(dialog, iRadioYes, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, true);
-
- // Default Check box setting
- GetDItem(dialog, iCheckBox, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, true);
-
- // Identify the window type
- SetWRefCon(dialog, (long)rGenericDlg1);
-
- // Maintain Undo
- gCanUndo = false;
-
- // Set up the Edit menu
- Adjust_Generic_EditMenu( dialog );
- }
- else
- DoError(eDialogNotFound, true);
- }
- }
-
-
- /************************ Init_GenericDlg2 ****************************/
-
- void Init_GenericDlg2( void )
- {
- short iType;
- Handle iHandle;
- Rect iRect;
- DialogPtr dialog;
- DialogPeek dlgPeek;
- Ptr dStorage;
-
- /* #define rGenericDlg2 130
- #define iDefaultOutline 2
- #define iEditText 5
- #define iRadioFirstDay 6
- #define iFirstCheckBox 14 */
-
-
- dStorage = NewPtrClear(kGenericDlgStorage);
-
- if (! dStorage)
- DoError(eNotEnoughMem, false);
- else
- {
- if (! gWindowsOpen)
- AdjustMenu(mFile, iClose, true);
- gWindowsOpen++;
-
- dialog = GetNewDialog(rGenericDlg2, dStorage, kMoveToFront);
- if (dialog)
- {
- SetPort(dialog);
-
- /* This dialog does not have a default button */
-
- // Set Insertion Caret
- SelIText(dialog, iEditText, 0,0);
- dlgPeek = (DialogPeek)dialog;
- gCur_DlgText = (dlgPeek->textH);
-
- // Default Radio setting
- GetDItem(dialog, iRadioFirstDay, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, true);
- SetCRefCon((ControlHandle)iHandle, (long)iRadioFirstDay);
-
- // Identify the window type
- SetWRefCon(dialog, (long)rGenericDlg2);
-
- // Maintain Undo
- gCanUndo = false;
-
- // Set up the Edit menu
- Adjust_Generic_EditMenu( dialog );
- }
- else
- DoError(eDialogNotFound, true);
- }
- }
-
-
- /********************* Handle_GenericDialog ****************************/
-
- void Handle_GenericDialog( EventRecord *event, long whichDlg )
- {
- short iHit = 0;
- DialogPtr dialog;
- My_UndoRec preEditState, postEditState;
-
- /* This routine contains the framework of handling dialog events,
- it farms out the processing as needed to specific handlers
- for the individual dialogs that come through */
-
- switch (whichDlg) // Process keyDown event for buttons if needed
- {
- case rGenericDlg1:
- if (Keydown_Button(event, ok, kOnlyButton))
- {
- Close_GenericDialog(FrontWindow());
- goto job_finished;
- }
- break;
- }
-
- preEditState = Get_UndoInfo(FrontWindow());
-
- if (DialogSelect(event, &dialog, &iHit))
- {
- switch (whichDlg)
- {
- case rGenericDlg1:
- ProcessEvt_GenericDlg1( dialog, iHit);
- break;
- case rGenericDlg2:
- ProcessEvt_GenericDlg2( dialog, iHit);
- break;
- }
- }
-
- if (iHit != ok)
- {
- postEditState = Get_UndoInfo(FrontWindow());
- if (postEditState.undoItem != preEditState.undoItem)
- {
- gCanUndo = false; // User changed active Edit Text item
- Adjust_Generic_EditMenu(FrontWindow());
- }
- else
- {
- if (preEditState.undoString[0] > postEditState.undoString[0]+1)
- {
- gUndo_Info = preEditState; // User selected & deleted chars
- gCanUndo = true;
- }
- Maintain_EditMenu(FrontWindow(), &preEditState);
- }
- }
-
- job_finished:
- ; // Keep the compiler happy
- }
-
-
- /********************** ProcessEvt_GenericDlg1 *******************************/
-
- void ProcessEvt_GenericDlg1( DialogPtr dialog, short iHit )
- {
- short iType;
- Handle iHandle;
- Rect iRect;
-
- switch (iHit)
- {
- case iRadioYes:
- case iRadioNo:
- GetDItem(dialog, iRadioYes, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, (!(GetCtlValue(iHandle))));
- GetDItem(dialog, iRadioNo, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, (!(GetCtlValue(iHandle))));
- break;
- case iCheckBox:
- GetDItem(dialog, iCheckBox, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, (!(GetCtlValue(iHandle))));
- break;
- case ok:
- Close_GenericDialog(dialog);
- break;
- }
- }
-
-
- /********************** ProcessEvt_GenericDlg2 *************************/
-
- void ProcessEvt_GenericDlg2( DialogPtr dialog, short iHit )
- {
- short iType;
- Handle iHandle, firstRadioH;
- Rect iRect;
- long lastRadioOn;
-
- switch (iHit)
- {
- case iRadioFirstDay:
- case iRadioFirstDay+1:
- case iRadioFirstDay+2:
- case iRadioFirstDay+3:
- case iRadioFirstDay+4:
- case iRadioFirstDay+5:
- case iRadioFirstDay+6:
- GetDItem(dialog, iRadioFirstDay, &iType, &firstRadioH, &iRect);
- lastRadioOn = GetCRefCon((ControlHandle)firstRadioH);
- GetDItem(dialog, iHit, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, (!(GetCtlValue(iHandle))));
- GetDItem(dialog, (short)lastRadioOn, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, (!(GetCtlValue(iHandle))));
- SetCRefCon(firstRadioH, (long)iHit);
- break;
- case iFirstCheckBox:
- case iFirstCheckBox+1:
- case iFirstCheckBox+2:
- GetDItem(dialog, iHit, &iType, &iHandle, &iRect);
- SetCtlValue(iHandle, (!(GetCtlValue(iHandle))));
- break;
- case ok:
- Close_GenericDialog(dialog);
- lastRadioOn = iRadioFirstDay;
- break;
- }
- }
-
-
- /********************* Close_GenericDialog ******************************/
-
- void Close_GenericDialog(DialogPtr dialog)
- {
- DialogPeek myDlgPeek;
-
- gWindowsOpen--;
- if (!gWindowsOpen)
- AdjustMenu(mFile, iClose, false);
-
- myDlgPeek = (DialogPeek)dialog;
- CloseDialog((DialogPtr)dialog);
- DisposePtr(myDlgPeek);
-
- gCanUndo = false;
- AdjustMenu(mEdit, 0, 0);
- DrawMenuBar();
- }
-
-
- /*********************** Adjust_Generic_EditMenu ****************************/
-
- void Adjust_Generic_EditMenu( DialogPtr dialog )
- {
- MenuHandle menu;
- TEHandle the_text;
- short textSize;
- Boolean menuActive = false;
- long text_scrap;
-
- menu = GetMHandle(mEdit);
- the_text = ((DialogPeek)dialog)->textH;
- textSize = (**the_text).teLength;
-
- // UNDO ITEM
- if (gCanUndo)
- {
- EnableItem(menu, iUndo);
- menuActive = true;
- }
- else
- DisableItem(menu, iUndo);
-
- // COPY, CUT, & CLEAR ITEMS
- if ((**the_text).selStart != (**the_text).selEnd)
- {
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iClear);
- menuActive = true;
- }
- else
- {
- DisableItem(menu, iCut);
- DisableItem(menu, iCopy);
- DisableItem(menu, iClear);
- }
-
- // PASTE ITEM
- text_scrap = TEGetScrapLen();
- if ((! text_scrap) || (text_scrap > (k255Characters - textSize)))
- DisableItem(menu, iPaste);
- else
- {
- EnableItem(menu, iPaste);
- menuActive = true;
- }
-
- // SELECT ALL ITEM
- if (textSize)
- {
- menuActive = true;
- EnableItem(menu, iSelectAll);
- }
- else
- DisableItem(menu, iSelectAll);
-
- // If any of the items are active, activate the menu
- if (menuActive)
- EnableItem(menu, 0);
- else
- DisableItem(menu, 0);
-
- DrawMenuBar(); // Enabling/Disabling entire menu requires MenuBar update
- }
-
-
- /************************** Do_Undo **********************************/
-
- void Do_Undo( DialogPtr dialog )
- {
- short iType;
- Handle iHandle;
- Rect iRect;
- My_UndoRec swapUndoInfo;
-
- swapUndoInfo = Get_UndoInfo((DialogPeek)dialog);
-
- GetDItem(dialog, gUndo_Info.undoItem, &iType, &iHandle, &iRect);
- SetIText(iHandle, gUndo_Info.undoString);
- SelIText(dialog, gUndo_Info.undoItem, gUndo_Info.undoSelStart,
- gUndo_Info.undoSelEnd);
- gUndo_Info = swapUndoInfo;
- Adjust_Generic_EditMenu(dialog);
- }
-
-
- /*************************** Get_UndoInfo ******************************/
-
- My_UndoRec Get_UndoInfo(DialogPeek dialog)
- {
- My_UndoRec undo_record;
- short iType;
- Handle iHandle;
- Rect iRect;
-
- /* The great thing about dialogs is they provide the necessary info
- right in the DialogRecord. The currectly active TE item and a handle
- to it. */
-
- // Maintain Undo_Info
- undo_record.undoItem = dialog->editField+1; // true editField# always +1
- GetDItem(dialog, undo_record.undoItem, &iType, &iHandle, &iRect);
- GetIText(iHandle, undo_record.undoString);
- undo_record.undoSelStart = (**gCur_DlgText).selStart;
- undo_record.undoSelEnd = (**gCur_DlgText).selEnd;
-
- return(undo_record);
- }
-
-
- /*********************** Maintain_EditMenu ****************************/
-
- void Maintain_EditMenu( DialogPtr dialog, My_UndoRec *preEditState )
- {
- Boolean adjust_menu = false;
- short pre_textSize, post_textSize;
- short pre_SelectionRange, post_SelectionRange;
- short iType;
- Handle iHandle;
- Rect iRect;
- My_UndoRec curUndoState;
-
- pre_textSize = (preEditState->undoString[0]);
- pre_SelectionRange = (preEditState->undoSelEnd) - (preEditState->undoSelStart);
-
- // If something happened in the edit text, make an adjustment
- post_textSize = (**gCur_DlgText).teLength;
- if ( post_textSize != pre_textSize){
- if ( ((pre_textSize) && (!post_textSize))
- || ((!pre_textSize) && (post_textSize)) )
- adjust_menu = true;
- }
- if (!adjust_menu){
- post_SelectionRange = (**gCur_DlgText).selEnd - (**gCur_DlgText).selStart;
- if (post_SelectionRange != pre_SelectionRange){
- if ( ((!post_SelectionRange) && (pre_SelectionRange))
- || ((post_SelectionRange) && (!pre_SelectionRange)) )
- adjust_menu = true;
- }
- }
-
- if (adjust_menu)
- Adjust_Generic_EditMenu(FrontWindow());
- }
-
-
- /********************** AdjustMenu ******************************/
-
- void AdjustMenu( short menuID, short item, Boolean switchOn )
- {
- MenuHandle menu;
-
- menu = GetMHandle(menuID);
- if (menu)
- {
- if (switchOn)
- EnableItem(menu, item);
- else
- DisableItem(menu, item);
- }
- }
-
-
- /************************* Keydown_Button *****************************/
-
- Boolean Keydown_Button( EventRecord *event, short item_number,
- short buttonKind)
- {
- Boolean buttonSelected = false;
- char theChar;
- DialogPtr dialog;
- ControlHandle item_h;
- long time;
- short itype;
- Rect box;
-
- /* This is a handy routine to process simple keyDown events for
- cancel/default button in dialogs. I got the idea from James W's
- Show_Help code. */
-
- if ((event->what == keyDown) || (event->what == autoKey))
- {
- theChar = event->message & charCodeMask;
-
- switch( buttonKind )
- {
- case kDefaultButton:
- if ((theChar == RETURN_CHAR) || (theChar == ENTER_CHAR))
- buttonSelected = true;
- break;
- case kCancelButton:
- if ((theChar == TILDE_CHAR) || (theChar == ESCAPE_CHAR))
- buttonSelected = true;
- break;
- case kOnlyButton:
- if ((theChar == RETURN_CHAR) || (theChar == ENTER_CHAR)
- || (theChar == TILDE_CHAR) || (theChar == ESCAPE_CHAR))
- buttonSelected = true;
- break;
- }
- if (buttonSelected)
- FlashButton(item_number);
- }
- return (buttonSelected);
- }
-
-
- /************************ FlashButton ********************************/
-
- void FlashButton( short item_number )
- {
- DialogPtr dialog;
- ControlHandle item_h;
- long time;
- short itype;
- Rect box;
-
- // Hilite's a button selected via a keyDown to show visual button selection
- dialog = FrontWindow();
- GetDItem( dialog, item_number, &itype, (Handle *)&item_h, &box );
- HiliteControl( item_h, inButton );
- Delay( 9L, &time );
- HiliteControl( item_h, 0 );
- }
-
-
- /************************* Close_AllDialogs *********************/
-
- void Close_AllDialogs( void )
- {
- short windowsOpen, loop;
- DialogPtr dialog;
-
- windowsOpen = gWindowsOpen;
- for (loop = 1; loop <= windowsOpen; loop++)
- {
- dialog = (DialogPtr)(FrontWindow());
- switch (GetWRefCon(dialog))
- {
- case rShowHelpDlg:
- Close_ShowHelp(dialog);
- break;
- case rGenericDlg1:
- case rGenericDlg2:
- Close_GenericDialog(dialog);
- break;
- }
- }
- }
-
-
- /************************** isPressed *******************************/
-
- short isPressed(unsigned short k )
- {
- /* CODE EXAMPLE FROM THINK REFERENCE */
- unsigned char km[16];
-
- // k = any keyboard scan code, 0-127
- GetKeys( (long *) km);
- return ( ( km[k>>3] >> (k & 7) ) & 1);
- }
-
-
- /********************* Can_OpenMoreWindows ******************************/
-
- Boolean Can_OpenMoreWindows( void )
- {
- Boolean canDo = true; // Assume OK
-
- if (gWindowsOpen == kMax_WindowsOpen)
- {
- canDo = false;
- DoError(eCantOpenNewWindow, false);
- }
-
- return(canDo);
- }
-
-
- /******************** DoError *******************************/
-
- void DoError( short err, Boolean fatal )
- {
- Str255 errString;
-
- GetIndString( errString, rErrStringsID, err );
- ParamText( errString, sEmptyString, sEmptyString, sEmptyString );
-
- if ( fatal )
- {
- gDone = true;
- StopAlert(rErrorAlertID, nil);
- }
- else
- NoteAlert(rErrorAlertID, nil );
- }
-